﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portfolio.IViews.Admin;
using Portfolio.BL.Articles;
using Portfolio.BL.ArticleCategories;
using Portfolio.BL.Interfaces;
using Portfolio.Entities;
using Portfolio.Presenters.Interfaces;

namespace Portfolio.Presenters.Admin
{
    public class AdminArticlesPresenter : IDefaultPresenter
    {
        private IAdminArticlesView _view;
        private IArticlesBL _listArticlesBL;
        private ArticleCategoriesBL _categoriesBL;

        public AdminArticlesPresenter(IAdminArticlesView view)
        {
            _view = view;
            _listArticlesBL = new ArticlesBL();
            _categoriesBL = new ArticleCategoriesBL();
            this.Initialize();
        }

        public void Initialize()
        {            
            this._view.LoadArticlesSortBySearch += new EventHandler<EventArgs>(_viewLoadArticlesSortBySearch);
            this._view.DeleteArticle += new EventHandler<EventArgs>(_viewDeleteArticle);
            this._view.LoadFieldsSearch += new EventHandler<EventArgs>(_viewLoadFieldsSearch);
        }
        

        private void _viewLoadArticlesSortBySearch(object sender, EventArgs e)
        {
            try
            {
                _view.ArticlesDataBind = _listArticlesBL.ReturnArticleBySearch(_view.SearchTitle, _view.SearchCategories, _view.SearchStatus, _view.SearchActive, _view.SortExpression, _view.SortDirection);                
            }
            catch (Exception ex)
            {
                _view.PublishException = "Błąd podczas ładowania artykułów! " + ex.Message;
            }
        }

        private void _viewLoadFieldsSearch(object sender, EventArgs e)
        {
            try
            {
                //_view.CategoriesDataSource = new IViews.Common.DataSourceContainer("Name", "ArticleCategoryID", _categoriesBL.ReturnCategoriesDDL(null), null);
                _view.StatusDataSource = new IViews.Common.DataSourceContainer("Value", "Key", _listArticlesBL.ReturnStatusEnum(), null);
                _view.ActiveDataSource = new IViews.Common.DataSourceContainer("Value", "Key", _listArticlesBL.ReturnActiveEnum(), null);
                _view.PageSizeDataSource = new IViews.Common.DataSourceContainer("Key", "Key", _listArticlesBL.ReturnPageSizeEnum(), 15);
            }
            catch (Exception ex)
            {
                _view.PublishException = "Błąd podczas ładowania wyszukiwarki! " + ex.Message;
            }
        }

        private void _viewDeleteArticle(object sender, EventArgs e)
        {
            try
            {
                _listArticlesBL.DeleteArticle(_view.Article);
                _view.IsArticleDel = true;
            }
            catch (Exception ex)
            {
                _view.PublishException = "Błąd podczas usuwania zaznaczonych artykułów! " + ex.Message;
            }
        }
    }
}
